<!-- eslint-disable -->
<template>
  <el-card class="box-card">
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin-bottom: 50px; margin-top: 50px"
      @click="dialogVisible = true"
      :disabled="role !== 'admin'"
      >添加考试项目</el-button
    >

    <el-table :data="examList" border style="width: 100%">
      <el-table-column
        prop="id"
        label="考试编号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type"
        label="考试名称"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="school"
        label="驾校"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="adress"
        label="考场地址"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="time"
        label="考试时间"
        width="200"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="img"
        label="考场图片"
        width="150"
        class="imgwrap"
        align="center"
      >
        <template v-slot="{ row }">
          <img :src="row.img" alt />
        </template>
      </el-table-column>

      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="{ row, index }">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            title="删除考试"
            @click="deleteEx(row)"
            :disabled="role !== 'admin'"
          ></el-button>
          <el-button
            type="warning"
            size="small"
            icon="el-icon-s-order"
            title="报名"
            :disabled="role !== 'user'"
            @click="toTakeExam(row)"
          ></el-button>

          <el-button
            type="info"
            size="small"
            icon="el-icon-info"
            title="详细信息"
            @click="examInfos(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加的弹出框 -->
    <el-dialog title="添加考试" :visible.sync="dialogVisible" width="30%">
      <el-form :model="form">
        <el-form-item label="考试名称">
          <el-input v-model="form.type" placeholder="请输入考试名称"></el-input>
        </el-form-item>

        <el-form-item label="考场名">
          <el-input v-model="form.school" placeholder="请输入考场名"></el-input>
        </el-form-item>
        <el-form-item label="考场地址">
          <el-input v-model="form.adress" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="考场图片">
          <el-input v-model="form.img" placeholder="请输入图片路径"></el-input>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.time"
          ></el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addExam">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 详情弹出层 -->
    <el-dialog title="考试详情" :visible.sync="infoStatus" width="50%">
      <el-form :model="examInfo">
        <el-form-item style="width: 600px" label="头像">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="examInfo.img" :src="examInfo.img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="考试名称">
          <el-input v-model="examInfo.type" placeholder="请输入考试名称"></el-input>
        </el-form-item>
        <el-form-item label="考场名称">
          <el-input v-model="examInfo.school" placeholder="请输入考场名称"></el-input>
        </el-form-item>
        <el-form-item label="考场地址">
          <el-input v-model="examInfo.adress" placeholder="请输入考场地址"></el-input>
        </el-form-item>
        <el-form-item label="考试时间">
          <el-input v-model="examInfo.time" placeholder="请输入考场地址"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
/* eslint-disable */
import { examList, addExam, deleteExam, getInfo, takeExam } from "@/api/exam";
import { mapState } from "vuex";

export default {
  name: "Class",
  data() {
    return {
      infoStatus: false,
      dialogVisible: false,
      pageNumber: 1, //当前页
      pagesize: 4, //分页的数据
      total: 0, //总页数
      examList: [],
      form: {
        type: "",
        school: "",
        adress: "",
        img: "",
        time: "",
      },
      examInfo: {
        type: "",
        school: "",
        adress: "",
        img: "",
        time: "",
      },
      takeForm: {
        nickname: "",
        username: "",
        type: "",
        school: "",
        adress: "",
        img: "",
        time: "",
      },
    };
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      this.getEaxmList();
    },
    handleCurrentChange(val) {
      this.pageNumber = val;
      this.getEaxmList();
    },
    async getEaxmList() {
      let res = await examList({ pagesize: this.pagesize, pageNumber: this.pageNumber });
      this.examList = res.data;
      this.examList.forEach(item => {
        
            item.time = this.dayjs(item.time).format("YYYY-MM-DD HH:mm:ss")
        });
      this.total = res.totalCount;
    },
    async addExam() {
      let res = await addExam(this.form);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.getEaxmList();
      }
      this.dialogVisible = false;
      this.form = {};
    },
    async deleteEx(row) {
      let res = await deleteExam(row.id);
      if (res.code == 200) {
        this.getEaxmList();
        this.$message({
          message: res.message,
          type: "success",
        });
      }
    },
    async examInfos(row) {
      this.infoStatus = true;
      let res = await getInfo(row.id);
      if (res.code == 200) {
        this.examInfo = res.data;
      }
    },
    async toTakeExam(row) {
      this.takeForm.username = this.info.username;
      this.takeForm.nickname = this.info.nickname;
      this.takeForm.type = row.type;
      this.takeForm.school = row.school;
      this.takeForm.adress = row.adress;
      this.takeForm.time = row.time;
      this.takeForm.img = row.img;
      let res = await takeExam(this.takeForm);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
      }
    },
  },
  mounted() {
    this.getEaxmList();
  },
  computed: {
    ...mapState({
      role: (store) => store.user.info.role,
      info: (store) => store.user.info,
    }),
  },
};
</script>

<style scoped>
img {
  width: 100px;
  height: 100px;
  margin-right: 15px;
}
.box-card {
  width: 1200px;
  margin-left: 50px;
  margin-top: 50px;
}
</style>
